/*
 * Copyright 2010 Biz-e (info@biz-e.nl)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package nl.biz_e.gwt.baselib.validation.client.processors;

import java.util.HashMap;
import java.util.Set;
import java.util.Vector;

import nl.biz_e.gwt.baselib.validation.client.ValidationProcessor;
import nl.biz_e.gwt.baselib.validation.client.Validator;

import com.google.gwt.user.client.ui.FocusWidget;

/**
 * TODO: Write type comment
 * 
 * @author Joost A. Bloemsma
 */
public abstract class BaseProcessor implements ValidationProcessor {
    private HashMap<FocusWidget, Vector<Validator>> validatorMap = new HashMap<FocusWidget, Vector<Validator>>();
    private Vector<String> messages = new Vector<String>();
    private boolean haltOnFail = false;

    /**
     * {@inheritDoc}
     */
    @Override
    public void addValidator(FocusWidget widget, Validator validator) {
        if (validatorMap.containsKey(widget)) {
            validatorMap.get(widget).add(validator);
        } else {
            Vector<Validator> v = new Vector<Validator>();
            v.add(validator);
            validatorMap.put(widget, v);
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Vector<String> getMessages() {
        return messages;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void reset() {
        Set<FocusWidget> widgets = validatorMap.keySet();
        for (FocusWidget w : widgets) {
            showError(w, false);
        }
        messages.clear();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean validate() {
        reset();
        Set<FocusWidget> widgets = validatorMap.keySet();
        boolean ok = true;
        for (FocusWidget w : widgets) {
            Vector<Validator> validators = validatorMap.get(w);
            for (Validator v : validators) {
                if (!v.validate(getValue(w))) {
                    ok = false;
                    messages.add(w.getTitle() + ": " + v.getErrorMessage());
                    showError(w, true);
                    if (haltOnFail)
                        return false;
                }
            }
        }
        return ok;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void setHaltOnFail(boolean halt) {
        haltOnFail = halt;
    }

    private Object getValue(FocusWidget widget) {
        return null;
    }

    private void showError(FocusWidget widget, boolean error) {
        // TODO Do something
    }

}
